package com.disthen.message.core.config;

import org.springframework.beans.factory.InitializingBean;


public class Listeners implements InitializingBean{
	/**
	 * 消息转换器
	 */
	private String msgConverter;
	/**
	 * 绑定的监听类 spring bean id
	 */
	private String listener;
	/**
	 * 监听器回调接口方法指定
	 */
	private String method;
	/**
	 * @see
	 * 消息队列名称
	 * 自动创建和销毁
	 */
	private String queue;
	/**
	 * @see
	 * topic | direct | public
	 */
	private String type;
	/**
	 * 订阅规则
	 * @since
	 * 在type = topic || type == direct时生效
	 * 默认值: #
	 */
	private String topic;
	/**
	 * 交换器名称
	 * IF 没有 THEN 自动创建
	 */
	private String exchangeName;
	
	/**
	 * 队列引用
	 * 自动新建绑定队列
	 */
	private String queueBeanRefName;
	/**
	 * 回应使用的交换器名称
	 */
	private String responseExchange;
	/**
	 * 回应路由keyword
	 * @default ""
	 */
	private String responseKey = "";
	
	public String getResponseExchange() {
		return responseExchange;
	}

	public void setResponseExchange(String responseExchange) {
		this.responseExchange = responseExchange;
	}

	public String getResponseKey() {
		return responseKey;
	}

	public void setResponseKey(String responseKey) {
		this.responseKey = responseKey;
	}

	public String getMsgConverter() {
		return msgConverter;
	}

	public void setMsgConverter(String msgConverter) {
		this.msgConverter = msgConverter;
	}

	public String getQueueBeanRefName() {
		return queueBeanRefName;
	}

	public void setQueueBeanRefName(String queueBeanRefName) {
		this.queueBeanRefName = queueBeanRefName;
	}

	public Listeners(){
	}
	
	public Listeners(String listener, String method, String queue, String type,
			String topic, String exchangeName) {
		super();
		this.listener = listener;
		this.method = method;
		this.queue = queue;
		this.type = type;
		this.topic = topic;
		this.exchangeName = exchangeName;
	}

	public void setListener(String listener) {
		this.listener = listener;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public void setQueue(String queue) {
		this.queue = queue;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public void setExchangeName(String exchangeName) {
		this.exchangeName = exchangeName;
	}

	public String getListener() {
		return listener;
	}

	public String getMethod() {
		return method;
	}

	public String getQueue() {
		return queue;
	}

	public String getType() {
		return type;
	}

	public String getTopic() {
		return topic;
	}

	public String getExchangeName() {
		return exchangeName;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if(this.listener == null || this.listener.isEmpty())
			throw new NullPointerException("监听Bean “listener” 属性不能为空");
		if(this.queue == null || this.queue.isEmpty()){
			this.queue = null;
		}
		if(this.exchangeName == null || this.exchangeName.isEmpty()){
			this.exchangeName = null;
		}
		if(this.method != null && this.method.isEmpty()){
			this.method = null;
		}
		if(this.type == null || this.type.isEmpty()){
			this.type = "topic";
		}
		if(this.topic == null || this.topic.isEmpty()){
			this.topic = this.type.equals("direct")?"":"#";
		}
		if(this.msgConverter == null || this.msgConverter.isEmpty()){
			this.msgConverter = null;
		}
	}
}
